package com.ukao.steward.base;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;

import com.ukao.steward.listener.OnItemClickListener;
import com.ukao.steward.util.CheckUtils;

/**
 * ViewHolder基类
 */
public class SuperViewHolder extends RecyclerView.ViewHolder   {

    private SparseArray<View> views;


    public SuperViewHolder(View itemView,final OnItemClickListener mOnItemClickListener) {
        super(itemView);
        this.views = new SparseArray<>();
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CheckUtils.isNull(mOnItemClickListener)){
                    mOnItemClickListener.onItemClick(getLayoutPosition());
                }

            }
        });
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T getView(int viewId) {
        View view = views.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }


}
